/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.facilitieselements;

import org.openaecfm.model.actor.Person;
import org.openaecfm.model.cost.CostValue;
import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.facilitieselements.Inventory;
import org.openaecfm.model.facilitieselements.InventoryTypeEnum;
import org.openaecfm.model.kernel.Group;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class InventoryIO extends
		AbstractEntityInstanceInputImpl<Inventory> implements
		IEntityInstanceOutput<Inventory> {
	private IEntityInstanceUtilInput<Group> utilInput;
	private IEntityInstanceUtilOutput<Group> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Group> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Inventory pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCINVENTORY",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set PredefinedType
		if (pojo.getPredefinedType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));
		}

		// Set Jurisdiction
		if (pojo.getJurisdiction() == null) {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getJurisdiction())));
		}

		// Set ResponsiblePersons
		if (pojo.getResponsiblePersons() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter respersons = new ListParameter();
			for (Person person : pojo.getResponsiblePersons()) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(person)));
			}
			ei.addParameter(respersons);
		}

		// Set LastUpdateDate
		if (pojo.getLastUpdateDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLastUpdateDate()
					.toString(), StringParameter.UTF_8));
		}

		// Set CurrentValue
		if (pojo.getCurrentValue() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getCurrentValue())));
		}

		// Set OriginalValue
		if (pojo.getOriginalValue() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getOriginalValue())));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Inventory doCreateObject() {
		return new Inventory();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Inventory pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Jurisdiction
		Parameter jurisdiction = sei.getParameter(6);
		if (jurisdiction.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) jurisdiction;
			pojo.setJurisdiction(appDataManager.getObject(e.toInstanceName()));
		}

		// Set ResponsiblePersons
		ListParameter respersons = (ListParameter) sei.getParameter(7);
		for (Parameter p : respersons) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			pojo.getResponsiblePersons().add(
					(Person) appDataManager.getObject(n.toInstanceName()));
		}

		// Set CurrentValue
		Parameter currentvalue = sei.getParameter(9);
		if (currentvalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) currentvalue;
			pojo.setCurrentValue((CostValue) appDataManager.getObject(n
					.toInstanceName()));
		}

		// Set OriginalValue
		Parameter orivalue = sei.getParameter(10);
		if (orivalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) orivalue;
			pojo.setOriginalValue((CostValue) appDataManager.getObject(n
					.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Group> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			Inventory pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		Parameter predefinedType = sei.getParameter(5);
		if (predefinedType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) predefinedType;
			pojo.setPredefinedType(InventoryTypeEnum.valueOf(e.toEnumString()));
		}

		// Set LastUpdateDate
		Parameter lastupdate = sei.getParameter(8);
		if (lastupdate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) lastupdate;
			pojo.setLastUpdateDate(new Date(s.toUTF8()));
		}
	}
}
